﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;

namespace WCFServiceWebRole1
{
    public class FriendDataServiceContext : TableServiceContext
    {
        private static String TableName = "Friends";

        public IQueryable<Friend> Friends
        {
            get
            {
                return this.CreateQuery<Friend>(TableName);
            }
        }

        public FriendDataServiceContext(string baseAddress, StorageCredentials credentials)
            : base(baseAddress, credentials)
        {
            this.ResolveType = x => typeof(Friend);
        }

        // insert the scientific job into table
        public void AddFriend(Friend f)
        {
            this.AddObject("Friends", f);
            this.SaveChanges();
        }

        public void createTestFriends()
        {
            Friend one = new Friend("1", "2", true);
            Friend two = new Friend("2", "3", true);
            Friend three = new Friend("3", "1", true);
            Friend four = new Friend("4", "5", true);

            this.AddObject("Friends", one);
            this.AddObject("Friends", two);
            this.AddObject("Friends", three);
            this.AddObject("Friends", four);
            this.SaveChanges();
        }

        public List<string> getFriendIds(string userId)
        {
            var friends = (from f in Friends
                           where f.RequesteeId == userId
                                 && f.RequestAccepted == true
                           select f).ToList().Select(f => f.RequesterId).ToList();

            friends.AddRange((from f in Friends 
                             where f.RequesterId == userId
                                   && f.RequestAccepted == true
                             select f).ToList().Select(f => f.RequesteeId));
            return friends;
        }

        /*// retrieve scientific job from table
        public ScientificJob GetScientificJob(string rowKey)
        {
            return (from r in this.ScientificJobs
                    where
                    r.RowKey == rowKey
                    select r).First();
        }

        // save changes of scientific job to table
        public void UpdateScientificJob(ScientificJob j)
        {
            this.UpdateObject(j);
            this.SaveChanges();
        }

        // delete scientific job from table
        public void DeleteScientificJob(ScientificJob j)
        {
            this.DeleteObject(j);
            this.SaveChanges();
        }*/
    }
}
